package com.iptv.grape.util;

import android.content.Context;

import com.iptv.grape.domain.Channel;
import com.iptv.grape.domain.ChannelBillTimeData;
import com.iptv.grape.event.IWindowPos;
import com.iptv.grape.event.LiveWindowDataArgs;
import com.iptv.grape.video.VideoSize;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

// 帮助类
public class CommUtil {

    public static String getShowTimeStringByEnd(int milliseconds) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String hms = formatter.format(milliseconds);
        return hms;
    }

    // 得到时分秒
    public static String getShowTime(int milliseconds) {
        String pat = "HH:mm:ss";
        if(milliseconds >= 60 * 60 * 1000 && milliseconds < 10 * 60 * 60 * 1000) {
            pat = "H:mm:ss";
        }else if(milliseconds < 60 * 60 * 1000) {
            pat = "mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pat);
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String hms = formatter.format(milliseconds);
        return hms;
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static VideoSize getVideoSize(Context context, IWindowPos args) {
        int width = 0;
        int height = 0;
        int left = 0;
        int top = 0;
        if(AppConstant.isTvStb()) {
            width = Math.round((args.getWidth() - 4));
            height = Math.round((args.getHeight() - 4));
            left = Math.round((args.getLeft() + 2));
            top = Math.round((args.getTop() + 2));
        }
        else {
            width = Math.round((args.getWidth() - 4) * args.getZoom());
            height = Math.round((args.getHeight() - 4) * args.getZoom());
            left = Math.round((args.getLeft() + 2) * args.getZoom());
            top = Math.round((args.getTop() + 2) * args.getZoom());
        }
        //return new VideoSize(px2dip(context,width),px2dip(context,height),px2dip(context,left),px2dip(context,top));
        //return new VideoSize(px2dip(context, width), px2dip(context, height), px2dip(context, left), px2dip(context, top));
        return new VideoSize(width,height,left,top);
    }


    public static Date toDate(String dateString) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(dateString);
            return date;
        } catch (Exception px) {
            px.printStackTrace();
        }
        return null;
    }

    //将Date型日期转化成指定格式的字符串形式。例如：yyyy-MM-dd HH:mm:ss
    public static String changeTime(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);

    }

    //
    public static Date addData(long  second){
        Date nowDate = new Date();
        long milliseconds = nowDate.getTime() + second * 1000;
        nowDate.setTime(milliseconds);
        return nowDate;
    }

    public static String getDataTimeByDataAcr(){
        return  CommUtil.formatDate(new Date(),"yyyyMMddHHmmss");
    }

    public static boolean equalsBillDate(Date date) {
        String pat = "yyyy-MM-dd HH:mm";
        if (date != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);//设置起时间
            cal.add(Calendar.MINUTE,1);
            date = cal.getTime();
            SimpleDateFormat htmlDf = new SimpleDateFormat(pat);
            System.out.println( htmlDf.format(new Date())+"===>>>"+htmlDf.format(date));
            return htmlDf.format(new Date()).equals(htmlDf.format(date));
        }
        return false;
    }

    public static void main(String[] args) {
        Date dt=  toDate("2019-11-1 13:29:00");
        boolean bb= equalsBillDate(dt);
        System.out.println(bb);


    }


    // HH:mm
    public static String formatDate(Date value, String pattern) {
        String pat = "yyyy-MM-dd HH:mm:ss";
        if (pattern != null) {
            pat = pattern;
        }
        String result = "";
        if (value != null) {
            SimpleDateFormat htmlDf = new SimpleDateFormat(pat);
            result = htmlDf.format(value).toString();
        }
        return result;
    }


    // 获取文件名中的日期
    // 输入 channelBillData_2019-09-04.json
    // 返回 2019-09-04
    public static String getFileNameStringDate(String str) {
        if(str != null && str.length() >= 15) {
            return str.substring(str.length() - 15, str.length() - 5);
        }
        return str;
    }

    public static boolean findDateStringHelp(List<String> dateStringList,String dateString){
        if(dateStringList == null){
            return false;
        }
        for(int i = 0; i < dateStringList.size(); i++){
            String tmpDateString = dateStringList.get(i);
            if(tmpDateString.equals(dateString)){
                return true;
            }
        }
        return false;
    }

    // 获得7天的时间字符串
    public static List<String> getDateString(){
        List<String> dateString = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 7; i++) {
            calendar.setTime(new Date());
            calendar.roll(Calendar.DAY_OF_YEAR, 0-i);
            Date prevDay = calendar.getTime();
            //dateString.add("channelBillData_" + sdf.format(prevDay) + ".json");
            dateString.add(sdf.format(prevDay));
        }
        return dateString;
    }

    public static List<ChannelBillTimeData> getChannelBillTimeDataList(Channel channel) {
        List<String> textList = new ArrayList<String>();
        textList.add("今天");
        textList.add("昨天");
        textList.add("前天");
        List<ChannelBillTimeData> dayList = new ArrayList<ChannelBillTimeData>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd");
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 7; i++) {
            calendar.setTime(new Date());
            calendar.roll(Calendar.DAY_OF_YEAR, 0-i);
            Date prevDay = calendar.getTime();
            ChannelBillTimeData day = new ChannelBillTimeData(channel, sdf1.format(prevDay),sdf.format(prevDay));
            if(i < textList.size()) {
                day.setText(textList.get(i));
            }
            dayList.add(day);
        }

        return dayList;
    }


    static int index = -1;
    public static  String GetPlayUrl(String str){

        List<String> urls = new ArrayList<String>();
        urls.add("http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super");
        //urls.add("http://jzvd.nathen.cn/342a5f7ef6124a4a8faf00e738b8bee4/cf6d9db0bd4d41f59d09ea0a81e918fd-5287d2089db37e62345123a1be272f8b.mp4");
        //urls.add("https://mov.bn.netease.com/open-movie/nos/mp4/2017/05/31/SCKR8V6E9_hd.mp4");
        index++;
        if(index >= urls.size()){
            index = 0;
        }
        return  urls.get(index);

    }
}
